
import numpy as np 
import csv
import os 
#from sklearn.cross_validation import train_test_split #已经丢弃的模块
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.neighbors import KNeighborsClassifier

data_filename = os.path.join('chp2',"ionosphere.data")
X = np.zeros((351,34),dtype=float) #数据集
y = np.zeros((351,),dtype=bool) #类别 1表示好 0表示坏 就两个类型的数据

with open(data_filename,'r') as fb:
    reader = csv.reader(fb)
    for i,row in enumerate(reader):
        data = [float(datnum) for datnum in row[:-1]]
        X[i] = data
        y[i] = row[-1]=='g'

#拆分训练数据集合测试数据集
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=14)

#创建估计器 近邻算法分类
estimator = KNeighborsClassifier()

estimator.fit(X_train,y_train) #用训练数据训练估计器
print("----------X_test-----------")
print(X_test)
y_predicted = estimator.predict(X_test) #测试数据,对测试数据进行实际的分类
print("---------y_predicted----------")
print(y_predicted)
a = y_test == y_predicted #矩阵比较 得到的是矩阵

accuracy = np.mean(a)*100 #计算平均值 也就得到数据是好的类型的概率 
print("the accuracy is {0:.1f}% ".format(accuracy))

scores = cross_val_score(estimator, X, y, scoring='accuracy')
average_accuracy = np.mean(scores) * 100
print("The average accuracy is {0:.1f}%".format(average_accuracy))

#调整算法的参数 测试正确率

avg_scores = []
all_scoress = []
param_values = list(range(1,21))
for i in param_values:
    estimator = KNeighborsClassifier(n_neighbors=i)
    scores = cross_val_score(estimator, X, y, scoring='accuracy')
    avg_scores.append(np.mean(scores)*100)
    all_scoress.append(scores)

'''
#画图表示
import matplotlib.pyplot as plt 

plt.plot(param_values,avg_scores)
plt.xlabel("n_neighbors")
plt.ylabel("avg_score")
plt.show()
'''

X_broken = np.array(X)

X_broken[:,::2] /= 10

#预处理
from sklearn.preprocessing import MinMaxScaler

X_transformed = MinMaxScaler().fit_transform(X_broken)
estimator = KNeighborsClassifier()
transformed_scores = cross_val_score(estimator, X_transformed, y,scoring='accuracy')
print("The average accuracy for is{0:.1f}%".format(np.mean(transformed_scores) * 100))

def print_score(scores):
    print("print_score::The pipeline scored an average accuracy for is {0:.1f}%".format(np.mean(scores) * 100))

#创建流水线

'''
流水线的输入为一连串的数据挖掘步骤，其中最后一步必须是估计器，前几步是转换器。输
入的数据集经过转换器的处理后，输出的结果作为下一步的输入。最后，用位于流水线最后一步
的估计器对数据进行分类。
'''

from sklearn.pipeline import Pipeline

#预处理，分类的流程写进流水线
scaling_pipline = Pipeline([
    ('scale',MinMaxScaler()),
    ("predict",KNeighborsClassifier())
])

scores = cross_val_score(scaling_pipline,X_broken,y,scoring='accuracy')
print_score(scores)